package com.whq.thrift.api.web.admin.db.consumelog;

import java.util.Map;

import org.apache.ibatis.jdbc.SqlBuilder;

import com.whq.common.StringUtil;

public class ConsumeLogSqlProvider {

	public String getSelectRecordsSql(Map<String, Object> parameter) {
		SqlBuilder.BEGIN();
		SqlBuilder.SELECT("*");
		SqlBuilder.FROM("t_consume_log_" + parameter.get("tableSeq"));

		if (parameter.get("time") != null && StringUtil.isNotBlank(parameter.get("time").toString())) {
			String startTime = (String) parameter.get("time") + " " + (String) parameter.get("start_time");
			String endTime = (String) parameter.get("time") + " " + (String) parameter.get("end_time");
			parameter.put("startTime", startTime);
			parameter.put("endTime", endTime);
			SqlBuilder.WHERE("time >= #{startTime} AND time <= #{endTime}");
		}

		if (parameter.get("api") != null && StringUtil.isNotBlank(parameter.get("api").toString())) {
			SqlBuilder.WHERE("run_method = #{api}");
		}

		if (parameter.get("code") != null && StringUtil.isNotBlank(parameter.get("code").toString())) {
			SqlBuilder.WHERE("invoked_method = #{code}");
		}

		if (parameter.get("flow_id") != null && StringUtil.isNotBlank(parameter.get("flow_id").toString())) {
			SqlBuilder.WHERE("flow_id = #{flow_id}");
		}

		if (parameter.get("result") != null && StringUtil.isNotBlank(parameter.get("result").toString())) {
			SqlBuilder.WHERE("result = #{result}");
		}

		SqlBuilder.ORDER_BY("time desc");
		return SqlBuilder.SQL() + " limit #{beginIndex}, #{maxCount} ";
	}

	public String getSelectCountSql(Map<String, Object> parameter) {
		SqlBuilder.BEGIN();
		SqlBuilder.SELECT("count(*)");
		SqlBuilder.FROM("t_consume_log_" + parameter.get("tableSeq"));

		if (parameter.get("time") != null) {
			String startTime = (String) parameter.get("time") + " " + (String) parameter.get("start_time");
			String endTime = (String) parameter.get("time") + " " + (String) parameter.get("end_time");
			parameter.put("startTime", startTime);
			parameter.put("endTime", endTime);
			SqlBuilder.WHERE("time >= #{startTime} AND time <= #{endTime}");
		}

		if (parameter.get("api") != null && StringUtil.isNotBlank(parameter.get("api").toString())) {
			SqlBuilder.WHERE("run_method = #{api}");
		}

		if (parameter.get("code") != null && StringUtil.isNotBlank(parameter.get("code").toString())) {
			SqlBuilder.WHERE("invoked_method = #{code}");
		}

		if (parameter.get("flow_id") != null && StringUtil.isNotBlank(parameter.get("flow_id").toString())) {
			SqlBuilder.WHERE("flow_id = #{flow_id}");
		}

		if (parameter.get("result") != null && StringUtil.isNotBlank(parameter.get("result").toString())) {
			SqlBuilder.WHERE("result = #{result}");
		}
		
		return SqlBuilder.SQL();
	}

	public String getStatByApiCodeSql(Map<String, Object> parameter) {
		SqlBuilder.BEGIN();
		SqlBuilder.SELECT("run_method as api, result , count(*) as count, avg(cost) as cost");
		SqlBuilder.FROM("t_consume_log_" + parameter.get("tableSeq"));

		SqlBuilder.WHERE("time >= #{start_time} AND time <= #{end_time}");

		if (parameter.get("api") != null && StringUtil.isNotBlank(parameter.get("api").toString())) {
			SqlBuilder.WHERE("run_method = #{api}");
		}

		if (parameter.get("result") != null && StringUtil.isNotBlank(parameter.get("result").toString())) {
			SqlBuilder.WHERE("result = #{result}");
		}

		SqlBuilder.GROUP_BY("run_method, result");
		return SqlBuilder.SQL();
	}

	public String getStatByCodeSql(Map<String, Object> parameter) {
		SqlBuilder.BEGIN();
		SqlBuilder.SELECT("result, count(*) as count, avg(cost) as cost");
		SqlBuilder.FROM("t_consume_log_" + parameter.get("tableSeq"));

		SqlBuilder.WHERE("time >= #{start_time} AND time <= #{end_time}");

		if (parameter.get("result") != null && StringUtil.isNotBlank(parameter.get("result").toString())) {
			SqlBuilder.WHERE("result = #{result}");
		}

		SqlBuilder.GROUP_BY("result");
		return SqlBuilder.SQL();
	}

	public String getIceCostTimeCountDataSql(Map<String, Object> parameter) {
		SqlBuilder.BEGIN();
		SqlBuilder.SELECT("run_method as api, result, count(*) as count");
		SqlBuilder.FROM("t_consume_log_" + parameter.get("tableSeq"));

		SqlBuilder.WHERE("time >= #{endTime} AND time <= #{startTime} ");
		SqlBuilder.GROUP_BY("run_method, result");
		return SqlBuilder.SQL();
	}
	
}
